using System;
using System.Collections;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

public class MarketBuyButton : MonoBehaviour
{
	public UIWidget holder;

	public UISprite buttonSprite;

	public UILabel title;

	public UILabel titleFree;

	public MarketPriceTag priceTag;

	public static Action OnSuccessedBuy;

	private Product productToBuy;

	private void OnClick()
	{
		Buy();
		MarketControllerUI.instance.mainButtons.SetMainButtons();
	}

	private void OnEnable()
	{
		CoroutineRunner.Instance.StartCoroutine(FadeIn());
	}

	private IEnumerator FadeIn()
	{
		holder.alpha = 0f;
		HOTween.To(holder, 0.25f, new TweenParms().Prop("alpha", 1f).UpdateType(UpdateType.TimeScaleIndependentUpdate));
		yield return CoroutineRunner.WaitForSeconds(0.26f);
		holder.alpha = 1f;
	}

	public void SetButton()
	{
		if (CupsManager.CurrentTier >= MarketControllerUI.instance.selectedProduct.tier)
		{
			if (!MarketControllerUI.instance.selectedProduct.Purchased)
			{
				if (MarketControllerUI.instance.selectedProduct.availableForBuy)
				{
					productToBuy = MarketControllerUI.instance.selectedProduct;
					SetButtonBuy(productToBuy, false);
					return;
				}
			}
			else if (MarketControllerUI.instance.selectedProduct.nextProduct != null && !MarketControllerUI.instance.selectedProduct.nextProduct.Purchased)
			{
				productToBuy = MarketControllerUI.instance.selectedProduct.nextProduct;
				productToBuy.ApplySale();
				if (CupsManager.CurrentTier >= productToBuy.tier)
				{
					SetButtonBuy(productToBuy, true);
					return;
				}
			}
		}
		holder.gameObject.SetActive(false);
	}

	private void SetButtonBuy(Product productToBuy, bool upgradeText)
	{
		priceTag.gameObject.SetActive(true);
		priceTag.SetPrice(productToBuy);
		holder.gameObject.SetActive(true);
		if (productToBuy is ProductCar && productToBuy.Purchased && MarketController.Instance.AllUpgradesAreBuyed(productToBuy.id))
		{
			upgradeText = false;
		}
		title.gameObject.SetActive(true);
		if (titleFree != null)
		{
			titleFree.gameObject.SetActive(false);
		}
		string text = ScriptLocalization.Get((!upgradeText) ? "Market/itemBuy" : "Market/itemUpgrade");
		ProductFreeUpgrade vehicleFreeUpgrade = MarketController.Instance.GetVehicleFreeUpgrade(productToBuy as ProductVehicleUpgrade);
		if (productToBuy is ProductVehicleUpgrade && vehicleFreeUpgrade != null && vehicleFreeUpgrade.Count > 0)
		{
			if (titleFree != null)
			{
				titleFree.text = ScriptLocalization.Get("Game_Common/CarM_freeUpgrade");
				titleFree.gameObject.SetActive(true);
			}
			title.gameObject.SetActive(false);
		}
		title.text = ((!string.IsNullOrEmpty(text)) ? text : "Нет локализации");
	}

	public void Buy()
	{
		if (MarketController.Instance.BuyProduct(productToBuy))
		{
			MarketControllerUI.instance.buyingMessage.ShowSuccesedBuy(productToBuy);
			MarketController.Instance.UpdateSaleCategory();
			if (productToBuy == MarketControllerUI.instance.selectedProduct.nextProduct)
			{
				MarketControllerUI.instance.productsPanel.selected = MarketControllerUI.instance.selectedProduct.nextProduct.id;
				MarketControllerUI.instance.productsPanel.ResetEquippedProductButton();
				MarketControllerUI.instance.productsPanel.selectedProductButton.InitProductButton(MarketControllerUI.instance.productsPanel, MarketControllerUI.instance.selectedProduct.nextProduct);
			}
			MarketControllerUI.instance.productsPanel.ResetEquippedProductButton();
			if (MarketControllerUI.instance.selectedProduct is ProductWeapon)
			{
				MarketControllerUI.instance.previewController.DefaultWeapon = MarketControllerUI.instance.selectedProduct;
			}
			if (OnSuccessedBuy != null)
			{
				OnSuccessedBuy();
			}
		}
		else if (!(productToBuy is ProductComsumable))
		{
			Debug.Log("Покупка не удалась");
			StartCoroutine(MarketControllerUI.instance.ShowBank(productToBuy.PriceIn));
		}
	}
}
